package com.fsgom.common.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

public final class ServerInfoUtils {
	private ServerInfoUtils() {
		throw new UnsupportedOperationException();
	}

	public static String getMachineIP() {
		try {
			InetAddress inetAddress = InetAddress.getLocalHost();
			byte[] ipAddress = inetAddress.getAddress();
			inetAddress = InetAddress.getByAddress(ipAddress);
			return inetAddress.toString().substring(1);
		} catch (UnknownHostException e) {
			return "";
		}
	}

	public static String getHostName() {
		String hostname = getFullHostName();

		if (hostname == null || hostname.indexOf('.') <= -1) {
			return hostname;
		}

		return hostname.substring(0, hostname.indexOf('.'));
	}

	public static String getFullHostName() {
		try {
			InetAddress inetAddress = InetAddress.getLocalHost();
			byte[] ipAddress = inetAddress.getAddress();
			inetAddress = InetAddress.getByAddress(ipAddress);
			return inetAddress.getHostName();
		} catch (UnknownHostException e) {
			return "";
		}
	}
}